var pmap = require("./propertyMap.js");
var commonView = require("./commonView.js");
var identify = require("./identify.js");
var queryRecord = require("./queryRecord.js");
var accounts = require("./accounts.js");
var userManage = require("./userManage.js");
var changePassword = require("./changePassword.js");
require("../bootstrap/modal.js");

var modalDialog, spinner;
// 登入时取得权限配置
var power, token;
var navData;
var queryNum = 0; // 用于判断是否第一次跳到查询界面
window.queryNavLi = "intraday";
(function() {
    $.fn.datetimepicker.dates['cn'] = {
        days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
        daysShort: ["日", "一", "二", "三", "四", "五", "六", "日"],
        daysMin: ["日", "一", "二", "三", "四", "五", "六", "日"],
        months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthsShort: ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"],
        meridiem: ['上午', '下午'],
        suffix: ['st', 'nd', 'rd', 'th'],
        today: "今天"
    };
})();
jQuery(document).ready(function($) {
    // console.log("doucment ready.......");
    $.post(homeServlet, {}, homeBack);
    initValue();
    // console.dir(navData);
    
});

// 权限接口返回
function homeBack(hdata) {    
    commonView.needLoading(hdata);
    
    if (hdata.code == "0000") { // 取权限配置成功
        window.merId = hdata.data.merId;
        if (hdata.data.roleId == "0") {
            power = {
                "verify": "all",
                "query": "all",
                "combination": "all",
                "accounts": "all",
                "users": "all"
            };
        } else {
            power = {
                "verify": "all",
                "query": "all",
                "combination": "all"
            };
        }
        $("#header-user_name").text(hdata.data.userName);
        navData = pmap.getNavData(power);
        commonView.setMainNav(navData);
        initPage();
    } else {
        location.href = "./index.html";
    }
    //  console.dir(hdata);
}

// 绑定动作
function bindEvent() {
    $("#mainNavUl li").on("click", function() {
        $("#mainNavUl li").removeClass("active");
        $(this).addClass("active");
        var navVal = $(this).data("value");
        var theNavLiVal;
        for (var i = 0, l = navData.length; i < l; i++) {
            if (navData[i].value == navVal) {
                if (navData[i].value == "query") {
                    theNavLiVal = queryNavLi;
                }
                commonView.setSecondNav(navData[i].child);
                break;
            }
        }
        if (navVal == "verify") {
            commonView.showStep("verify0");
            identify.bindEventAgain();
            commonView.setPageTitle("信用查询", "icon-tint");
            commonView.setPagerPath(["查询", "选择组合"]);
        } else if (navVal == "query") {
            commonView.setPageTitle("查询记录", "icon-search");
            if (queryNavLi == "history") {
                commonView.showStep("query1");
            } else {
                commonView.showStep("query0");
            }
            queryRecord.bindEventAgain();
            if (queryNum == 0) {
                commonView.setPagerPath(["查询", "查询当天记录"]);

                queryRecord.getTodayRecord();
                queryNum = 1;
            }
        } else if (navVal == "combination") {
            commonView.showStep("combination");
            commonView.setPageTitle("查询组合", "icon-beaker");
            commonView.setPagerPath(["组合", "现有组合"]);
            $("#setSecondNav a[data-value=existing]").on("click", function() {
                commonView.showStep("combination");
            });
            $("#setSecondNav a[data-value=setup]").on("click", function() {
                commonView.showStep("combination1");
            });
        } else if (navVal == "accounts") {
            commonView.setPageTitle("帐务管理", "icon-shield");
            commonView.setPagerPath(["帐务", "计次管理"]);
            commonView.showStep("accounts0");
            accounts.getAccounts();
            accounts.getAccountsRecord();
        } else if (navVal == "users") {
            commonView.setPageTitle("用户管理", "icon-sitemap");
            commonView.setPagerPath(["用户", "机构管理"]);
            commonView.showStep("userManage");
            userManage.getUserTableData();
        }
    });
    $("#sign-out").on("click", function() {
        $.post(singOutServert, {}, function(singOutData) {
            location.href = "./index.html";
        });
    });
    $("#chang-password_btn").on("click", function() {
        commonView.setPageTitle("修改密码", "icon-sitemap");
        commonView.setPagerPath(["修改密码"]);
        commonView.showStep("changePassword");
        var navDataIndex = pmap.mainNavmap.length - 1;
        commonView.setSecondNav(pmap.mainNavmap[navDataIndex].child);
    });

}

// 初始化页
function initPage() {
    
    commonView.setHeadDate();
    bindEvent();
    identify.initIdentify();
    queryRecord.initQueryRecord();
    accounts.initAccouts();
    userManage.initManageValue();
    $(".validate-form").Validform({
        tiptype: 2
    });
    modalDialog = $("#loding .modal-dialog")[0];
    spinner = new Spinner().spin();
    spinner.spin(modalDialog);
    changePassword.bindChangeEvent();
}

/*// 判断是否登录
function isSignIn() {
    token = $.cookie("token");
    var powerString = $.cookie('power');
    if (!powerString) {
        location.href = "./index.html";
    }
    if (powerString) {
        power = JSON.parse(powerString);
    }
}*/



// 初始化变量
function initValue() {
    
}